package com.leetcode.p200;

import java.util.Arrays;

public class Test213 {

    public int rob(int[] nums) {
        if (nums.length <= 1) {
            return nums[0];
        }

        int num1 = help(Arrays.copyOfRange(nums, 0, nums.length - 1));
        int num2 = help(Arrays.copyOfRange(nums, 1, nums.length));
        return Math.max(num1, num2);
    }

    /**
     * 采用 滚动数组 机制，节省内存，其实上面的更通俗易懂
     */
    public int help(int[] nums) {
        if (nums.length == 0) {
            return nums[0];
        }

        if (nums.length == 1) {
            return nums[0];
        }

        if (nums.length == 2) {
            return Math.max(nums[0], nums[1]);
        }

        int pre = nums[0];
        int next = Math.max(nums[0], nums[1]);
        int result = 0;

        for (int i = 2; i < nums.length; i++) {
            result = Math.max(nums[i] + pre, next);
            pre = next;
            next = result;
        }

        return next;
    }

    public static void main(String[] args) {
        Test213 test = new Test213();
//        int result = test.rob(new int[]{1, 2, 3, 1});
        int result = test.rob(new int[]{2, 3, 2});
        System.out.println(result);
    }

}
